import axios, { type InternalAxiosRequestConfig } from "axios";
import { showFailToast,  } from "vant";

const instance = axios.create({
    baseURL: 'https://meikou-api.itheima.net',
    timeout:10000
})
instance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
    const user = bf.getUser()
    if (user.token) {
        config.headers.Authorization=`Bearer ${user.token}`
    }
    else {
        showFailToast('认证失败')
        return Promise.reject(new Error('认证失败'))
    }
    
    return config
    
}, e=>Promise.reject(e))
instance.interceptors.response.use((response) => {
   const res=response.data
       if(res.code==='1'){
        return res.result
       }
       return Promise.reject(new Error())
    
    
},e=>Promise.reject(e))
export class RequestAxios{
    static get<T>(url: string, params?: object) {
        return instance.get<null,T>(url,{params})
    }
    static post<T>(url: string, data?: object) {
        return instance.post<null,T>(url,data)
    }
      static delete<T>(url: string, params?: object) {
        return instance.delete<null,T>(url,{params})
    }
    static put<T>(url: string, data?: object) {
        return instance.put<null,T>(url,data)
    }
    static uploadFile<T>(url:string,data?:object){
        return instance.post<null, T>(url, data, {
          headers: {
            "Content-Type": "multipart/form-data",
          },
        });
    }
}